// import {  __private } from "cc";
import { Res } from "./Res";

export default class ResLoadQueue {
    private queue: Array<any> = [];
    private objectDict: { [key: string]: any } = {};

    /**
     * 清理所有任务
     */
    public clear() {
        this.queue.splice(0, this.queue.length);
        let objectDict = this.objectDict;
        for (let key in objectDict) {
            Res.recycle(objectDict[key]);
        }
        this.objectDict = {};
    }

    /**
     * 获取对象
     * @param name 
     * @returns 
     */
    public getObject<T>(name: string): T {
        return this.objectDict[name] as T;
    }

    /**
     * 添加加载任务到队列上
     * @param func 
     * @param args 
     */
    public add(func: Function, ...args: any[]) {
        this.queue.push({ func: func, args: args });
    }

    /**
     * 加载预设资源
     * @param path 
     * @param callback 
     */
    public addObject(path: string, callback?: Function, that?: any) {
        this.add(this.loadObject, path, callback, that);
    }

    /**
     * 加载图片精灵资源
     * @param path 
     * @param callback 
     */
    public addSpriteFrame(path: string, callback?: Function, that?: any) {
        this.add(this.loadSpriteFrame, path, callback, that);
    }

    /**
     * 加载FGUI的包
     * @param path 
     * @param callback 
     */
    public addPackage(path: string, callback?: Function, that?: any) {
        this.add(this.loadPackage, path, callback, that);
    }

    /**
     * 加载资源
     * @param target 
     */
    public async load() {
        let queue = this.queue;
        for (let i = 0; i < queue.length; ++i) {
            let temp = queue[i];
            let func = temp.func;
            let args = temp.args;
            await func.call(this, ...args);
        }
    }

    /**
     * 加载预设
     * @param path 
     * @param callback 
     */
    protected async loadSpriteFrame(path: string, callback?: Function, that?: any) {
        if (this.objectDict[path]) {
            console.error('资源已经加载了：' + path);
            return;
        }
        let asset = await Res.loadSpriteFrameAsync(path);
        this.objectDict[path] = asset;
        callback?.call(that, asset);
        return asset;
    }

    /**
     * 加载预设
     * @param path 
     * @param callback 
     */
    protected async loadObject(path: string, callback?: Function, that?: any) {
        if (this.objectDict[path]) {
            console.error('资源已经加载了：' + path);
            return;
        }
        let asset = await Res.spawnAsync(path);
        this.objectDict[path] = asset;
        callback?.call(that, asset);
        return asset;
    }

    /**
     * 加载UI包
     * @param path 
     * @param callback 
     */
    protected async loadPackage(path: string, callback?: Function, that?: any) {
        if (this.objectDict[path]) {
            console.error('资源已经加载了：' + path);
            return;
        }
        let asset = await Res.loadUIPackageAsync(path);
        this.objectDict[path] = asset;
        callback?.call(that, asset);
        return asset;
    }
}
